/**
 * @author harry
 * @date 2022/3/2/11:14
 */

package com.serviceOrchestration.atomic.units;

import com.serviceOrchestration.annotation.AtomicService;
import com.serviceOrchestration.atomic.AtomicUnitContext;
import com.serviceOrchestration.pojo.User;
import com.serviceOrchestration.atomic.AtomicUnit;
import com.serviceOrchestration.factory.AtomicFactory;

import java.text.SimpleDateFormat;
import java.util.Date;

@AtomicService(
        value = AtomicFactory.BEAN_PREFIX + 1,
        name = "证件审查服务",
        description = "用户证件有效性检查"
)
public class IdValidityReviewUnit extends AtomicUnit {

    private final SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");

    @Override
    protected boolean doHandle(AtomicUnitContext context) {
        User user = (User) context.getContext("user");
        String today = sdf.format(new Date());
        String expire = sdf.format(user.getValidity());
        if (today.compareTo(expire) > 0) {
            context.setError("身份证明已过期");
            return false;
        }
        return true;
    }
}
